package data;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.ResourceBundle;

import rmi.ServeurRmiInterface;
import bean.Medecin;
import bean.Medicament;
import bean.Ordonnance;
import bean.Patient;
import bean.Prescription;
import bean.RmiBean;

public class Rmi {
	private static String ficConfig = "rmi";

	public static ServeurRmiInterface connect() {
		ResourceBundle rs = ResourceBundle.getBundle(ficConfig);
		int port = Integer.parseInt(rs.getString("port"));
		String nom = rs.getString("nom");
		ServeurRmiInterface serveur = null;
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			serveur = (ServeurRmiInterface) registry.lookup(nom);
		} catch (AccessException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}

		return serveur;
	}

	public static Medecin authentification(Medecin medecin)
			throws RemoteException {
		ServeurRmiInterface serveur = Rmi.connect();
		return serveur.authentification(medecin);
	}

	public static ArrayList<Patient> recherchePatient(String nom,
			String prenom, Integer debut, Integer nombre) {
		ServeurRmiInterface serveur = Rmi.connect();
		ArrayList<Patient> list = null;
		try {
			list = serveur.rechercherPatient(nom, prenom, debut, nombre);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.recherchePatient() : "
					+ e.getMessage());
		}
		return list;
	}

	public static ArrayList<Prescription> recherchePrescription(
			Integer idOrdonnance, Integer debut, Integer nombre) {
		ServeurRmiInterface serveur = Rmi.connect();
		ArrayList<Prescription> list = null;
		try {
			list = serveur.rechercherPrescription(idOrdonnance, debut, nombre);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.recherchePatient() : "
					+ e.getMessage());
		}
		return list;
	}

	public static ArrayList<Medicament> rechercheMedicament(String nom,
			Integer debut, Integer nombre) {
		ServeurRmiInterface serveur = Rmi.connect();
		ArrayList<Medicament> list = null;
		try {
			list = serveur.rechercherMedicament(nom, debut, nombre);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.rechercheMedicament() : "
					+ e.getMessage());
		}
		return list;
	}

	public static ArrayList<Ordonnance> rechercheOrdonnance(Integer idPatient,
			Integer debut, Integer nombre) {
		ServeurRmiInterface serveur = Rmi.connect();
		ArrayList<Ordonnance> list = null;
		try {
			list = serveur.rechercherOrdonnance(idPatient, debut, nombre);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.rechercheMedicament() : "
					+ e.getMessage());
		}

		return list;
	}

	public static int ajouter(RmiBean rmiBean) {
		ServeurRmiInterface serveur = Rmi.connect();
		int res = -1;
		try {
			res = serveur.ajouter(rmiBean);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.ajouter() : " + e.getMessage());
		}
		return res;
	}

	public static boolean modifier(RmiBean rmiBean) {
		ServeurRmiInterface serveur = Rmi.connect();
		boolean res = false;
		try {
			res = serveur.modifier(rmiBean);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.modifier() : " + e.getMessage());
		}
		return res;
	}

	public static boolean supprimer(RmiBean rmiBean) {
		ServeurRmiInterface serveur = Rmi.connect();
		boolean res = false;
		try {
			res = serveur.supprimer(rmiBean);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.supprimer() : " + e.getMessage());
		}
		return res;
	}

	public static Medecin getMedecin(Integer id) {
		ServeurRmiInterface serveur = Rmi.connect();
		Medecin medecin = null;
		try {
			medecin = serveur.getMedecin(id);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.getMedecin() : " + e.getMessage());
		}
		return medecin;
	}

	public static Patient getPatient(Integer id) {
		ServeurRmiInterface serveur = Rmi.connect();
		Patient patient = null;
		try {
			patient = serveur.getPatient(id);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.getPatient() : " + e.getMessage());
		}
		return patient;
	}

	public static Medicament getMedicament(Integer id) {
		ServeurRmiInterface serveur = Rmi.connect();
		Medicament medicament = null;
		try {
			medicament = serveur.getMedicament(id);
		} catch (RemoteException e) {
			System.out.println("data.Rmi.getMedicament() : " + e.getMessage());
		}
		return medicament;
	}

}
